<?php
class sp_redis_html{
	
	function sp_redis_menu_delete(){
		if (get_option('sp_redis_url') == ''){
			?>
<div class="wrap">
    <h1>Redis清除缓存</h1>
	<p>注意:此插件需要配合带有Redis缓存功能的ndex.php,可以到这里下载:https://gitee.com/haebong/wordpress-redis-cache</p>
	<p><font color=red>请先设置后再使用</font></p>
</div>
		<?php
		}else{
	?>
<div class="wrap">
    <h1>Redis清除缓存</h1>
	<p>注意:此插件需要配合带有Redis缓存功能的ndex.php,可以到这里下载:https://gitee.com/haebong/wordpress-redis-cache</p>
	<form method="post" action="">
		<table class="form-table">
			<tr>
				<th>完整地址:</th>
				<td><code><?php echo get_option('sp_redis_url').get_option('sp_redis_command'); ?></code></td>
			</tr>
			<tr>
				<th>网址URL:</th>
				<td><code><?php echo get_option('sp_redis_url'); ?></code></td>
			</tr>
			<tr>
				<th>删除指令:</th>
				<td><code><?php echo get_option('sp_redis_command'); ?></code></td>
			</tr>
			<tr>
				<th>成功指令:</th>
				<td><code><?php echo get_option('sp_redis_request'); ?></code></td>
			</tr>
			<tr>
				<th>无缓存指令:</th>
				<td><code><?php echo get_option('sp_redis_request_no'); ?></code></td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" name="sp_redis_delete" class="button-primary" value="清除Redis缓存"/>
		</p>
	</form>
</div>
<?php
		}
	}

function sp_redis_menu_seting(){
	if (!empty(get_option('sp_redis_url'))){
		$home_url = get_option('sp_redis_url');
	}else{
		$home_url = home_url().'/';
	}
?>
<div class="wrap">
    <h1>设置</h1>
	<form method="post" action="">
		<table class="form-table">
			<tr>
				<th>redis客户端路径:</th>
				<td>
				<select id="sp_redis_predis">
				<option value="<?php echo str_replace('\\','/',realpath(dirname(__FILE__).'/../../../../')); ?>">网站根目录</option>
				<option value="<?php echo str_replace('\\','/',realpath(dirname(__FILE__).'/../../../../redis/')); ?>">/redis/</option>
            </select>
			</td>
        </tr>
			<tr>
				<th>redis客户端路径:</th>
				<td>
					<input name="sp_redis_predis_url" type="text" id="sp_redis_predis_url" class="regular-text" value="<?php echo get_option('sp_redis_predis'); ?>">
					* 可选择predis所在路径,也可手动输入.
				</td>
			</tr>
			<tr>
				<th>网址URL:</th>
				<td><input name="sp_redis_url" type="text" id="sp_redis_url" class="regular-text" value="<?php echo $home_url; ?>">
				* 网站URL,例如https://6ns.net/,必须以/结尾
				</td>
			</tr>
			<tr>
				<th>清除指令:</th>
				<td><input name="sp_redis_command" type="text" id="sp_redis_command" class="regular-text" value="<?php echo get_option('sp_redis_command'); ?>">
				* 指令在index.php里设置,输入完整的清除指令,例如?c=yy
				</td>
			</tr>
			<tr>
				<th>成功指令:</th>
				<td><input name="sp_redis_request" type="text" id="sp_redis_request" class="regular-text" value="<?php echo get_option('sp_redis_request'); ?>">
				* 缓存清除成功后服务器输出的内容,用于判断状态</td>
			</tr>
			<tr>
				<th>无缓存指令:</th>
				<td><input name="sp_redis_request_no" type="text" id="sp_redis_request_no" class="regular-text" value="<?php echo get_option('sp_redis_request_no'); ?>">
				* 缓存没有被清除或无缓存时,用于判断状态</td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" name="sp_redis_save" class="button-primary" value="保存到数据库"/>
		</p>
	</form>
</div>
<script>
	function $(o){return document.getElementById(o)}
	$('sp_redis_predis').onchange = function(){
		$('sp_redis_predis_url').value = this.value;
    }
</script>
<?php
	}
	function sp_redis_server_info(){
	if (get_option('sp_redis_predis') == ''){
			?>
<div class="wrap">
    <h1>Redis清除缓存</h1>
	<p>注意:此插件需要配合带有Redis缓存功能的ndex.php,可以到这里下载:https://gitee.com/haebong/wordpress-redis-cache</p>
	<p><font color=red>请先设置后再使用</font></p>
</div>
		<?php
		}else{
			$predis_url = get_option('sp_redis_predis');
			include($predis_url);
			$redis = new Predis\Client('');		
			$redis_info = $redis->info();
	?>
<div class="wrap">
    <h1>Redis服务器状态</h1>
	<table class="form-table">
		<tr>
			<th>redis版本:</th>
			<td><code><?php echo $redis_info['redis_version']; ?></code></td>
		</tr>
		<tr>
			<th>redis编译ID:</th>
			<td><code><?php echo $redis_info['redis_build_id']; ?></code></td>
		</tr>
		<tr>
			<th>redis运行模式:</th>
			<td><code><?php echo $redis_info['redis_mode']; ?></code></td>
		</tr>
		<tr>
			<th>操作系统:</th>
			<td><code><?php echo $redis_info['os']; ?></code></td>
		</tr>
		<tr>
			<th>架构:</th>
			<td><code><?php echo $redis_info['arch_bits'].' 位'; ?></code></td>
		</tr>
		<tr>
			<th>多路复用API:</th>
			<td><code><?php echo $redis_info['multiplexing_api']; ?></code></td>
		</tr>
		<tr>
			<th>当前进程ID:</th>
			<td><code><?php echo $redis_info['process_id']; ?></code></td>
		</tr>
		<tr>
			<th>进程id:</th>
			<td><code><?php echo $redis_info['run_id']; ?></code></td>
		</tr>
		<tr>
			<th>端口:</th>
			<td><code><?php echo $redis_info['tcp_port']; ?></code></td>
		</tr>
		<tr>
			<th>运行时间:</th>
			<td><code><?php echo $redis_info['uptime_in_seconds'].' 秒'; ?></code></td>
		</tr>
		<tr>
			<th>运行时间:</th>
			<td><code><?php echo $redis_info['uptime_in_days'].' 天'; ?></code></td>
		</tr>
		<tr>
			<th>Redis内部调度:</th>
			<td><code><?php echo $redis_info['hz']; ?></code></td>
		</tr>
		<tr>
			<th>LRU自增时钟:</th>
			<td><code><?php echo date('Y-m-d H:i:s',$redis_info['lru_clock']); ?></code></td>
		</tr>
		<tr>
			<th>已连接客户端数量:</th>
			<td><code><?php echo $redis_info['connected_clients']; ?></code></td>
		</tr>
		<tr>
			<th>最大输出列表:</th>
			<td><code><?php echo $redis_info['client_longest_output_list']; ?></code></td>
		</tr>
		<tr>
			<th>最大输出缓存:</th>
			<td><code><?php echo $redis_info['client_biggest_input_buf']; ?></code></td>
		</tr>
		<tr>
			<th>阻止客户端:</th>
			<td><code><?php echo $redis_info['blocked_clients']; ?></code></td>
		</tr>
		<tr>
			<th>Redis总内存:</th>
			<td><code><?php echo $redis_info['used_memory_human']; ?></code></td>
		</tr>
		<tr>
			<th>已分配总内存:</th>
			<td><code><?php echo $redis_info['used_memory_rss_human']; ?></code></td>
		</tr>
		<tr>
			<th>内存消耗峰值:</th>
			<td><code><?php echo $redis_info['used_memory_peak_human']; ?></code></td>
		</tr>
		<tr>
			<th>系统总内存:</th>
			<td><code><?php echo $redis_info['total_system_memory_human']; ?></code></td>
		</tr>
		<tr>
			<th>Lua已使用内存:</th>
			<td><code><?php echo $redis_info['used_memory_lua_human']; ?></code></td>
		</tr>
		<tr>
			<th>最大内存:</th>
			<td><code><?php echo $redis_info['maxmemory_human']; ?></code></td>
		</tr>
		<tr>
			<th>最大内存策略:</th>
			<td><code><?php echo $redis_info['maxmemory_policy']; ?></code></td>
		</tr>
		<tr>
			<th>内存碎片比例:</th>
			<td><code><?php echo $redis_info['mem_fragmentation_ratio']; ?></code></td>
		</tr>
		<tr>
			<th>内存分配器:</th>
			<td><code><?php echo $redis_info['mem_allocator']; ?></code></td>
		</tr>
		<tr>
			<th>装载:</th>
			<td><code><?php echo $redis_info['loading']; ?></code></td>
		</tr>
		<tr>
			<th>最后一次执行命令次数:</th>
			<td><code><?php echo $redis_info['rdb_changes_since_last_save']; ?></code></td>
		</tr>
		<tr>
			<th>是否在进行gbsave:</th>
			<td><code><?php echo $redis_info['rdb_bgsave_in_progress']; ?></code></td>
		</tr>
		<tr>
			<th>最后一次保存时间:</th>
			<td><code><?php echo date('Y-m-d H:i:s',$redis_info['rdb_last_save_time']); ?></code></td>
		</tr>
		<tr>
			<th>上次保存状态:</th>
			<td><code><?php echo $redis_info['rdb_last_bgsave_status']; ?></code></td>
		</tr>
		<tr>
			<th>上次保存耗时:</th>
			<td><code><?php echo $redis_info['rdb_last_bgsave_time_sec']; ?></code></td>
		</tr>
		<tr>
			<th>保存RDB花费时间:</th>
			<td><code><?php echo $redis_info['rdb_current_bgsave_time_sec']; ?></code></td>
		</tr>
		<tr>
			<th>aof是否启用:</th>
			<td><code><?php echo $redis_info['aof_enabled']; ?></code></td>
		</tr>
		<tr>
			<th>是否写入AOF文件:</th>
			<td><code><?php echo $redis_info['aof_rewrite_in_progress']; ?></code></td>
		</tr>
		<tr>
			<th>gbsave结束后开始rewrite:</th>
			<td><code><?php echo $redis_info['aof_rewrite_scheduled']; ?></code></td>
		</tr>
		<tr>
			<th>上次写入rewrite:</th>
			<td><code><?php echo date('Y-m-d H:i:s',$redis_info['aof_last_rewrite_time_sec']); ?></code></td>
		</tr>
		<tr>
			<th>记录rewrite所用时间:</th>
			<td><code><?php echo $redis_info['aof_current_rewrite_time_sec']; ?></code></td>
		</tr>
		<tr>
			<th>上次读取状态:</th>
			<td><code><?php echo $redis_info['aof_last_bgrewrite_status']; ?></code></td>
		</tr>
		<tr>
			<th>上次写入状态:</th>
			<td><code><?php echo $redis_info['aof_last_write_status']; ?></code></td>
		</tr>
		<tr>
			<th>已链接总数量:</th>
			<td><code><?php echo $redis_info['total_connections_received']; ?></code></td>
		</tr>
		<tr>
			<th>已执行总数量:</th>
			<td><code><?php echo $redis_info['total_commands_processed']; ?></code></td>
		</tr>
		<tr>
			<th>每秒执行命令数:</th>
			<td><code><?php echo $redis_info['instantaneous_ops_per_sec']; ?></code></td>
		</tr>
		<tr>
			<th>流入流量:</th>
			<td><code><?php echo $redis_info['total_net_input_bytes'].'bytes'; ?></code></td>
		</tr>
		<tr>
			<th>流出流量:</th>
			<td><code><?php echo $redis_info['total_net_output_bytes'].'bytes'; ?></code></td>
		</tr>
		<tr>
			<th>每秒读取:</th>
			<td><code><?php echo $redis_info['instantaneous_input_kbps'].'bytes'; ?></code></td>
		</tr>
		<tr>
			<th>每秒写入:</th>
			<td><code><?php echo $redis_info['instantaneous_output_kbps'].'bytes'; ?></code></td>
		</tr>
		<tr>
			<th>超过连接数被拒绝数:</th>
			<td><code><?php echo $redis_info['rejected_connections']; ?></code></td>
		</tr>
		<tr>
			<th>同步全部:</th>
			<td><code><?php echo $redis_info['sync_full']; ?></code></td>
		</tr>
		<tr>
			<th>部分已同步:</th>
			<td><code><?php echo $redis_info['sync_partial_ok']; ?></code></td>
		</tr>
		<tr>
			<th>部分同步错误:</th>
			<td><code><?php echo $redis_info['sync_partial_err']; ?></code></td>
		</tr>
		<tr>
			<th>已过期key:</th>
			<td><code><?php echo $redis_info['expired_keys']; ?></code></td>
		</tr>
		<tr>
			<th>已删除key:</th>
			<td><code><?php echo $redis_info['evicted_keys']; ?></code></td>
		</tr>
		<tr>
			<th>Key命中次数:</th>
			<td><code><?php echo $redis_info['keyspace_hits']; ?></code></td>
		</tr>
		<tr>
			<th>Key未命中次数:</th>
			<td><code><?php echo $redis_info['keyspace_misses']; ?></code></td>
		</tr>
		<tr>
			<th>频道数量:</th>
			<td><code><?php echo $redis_info['pubsub_channels']; ?></code></td>
		</tr>
		<tr>
			<th>模式数量:</th>
			<td><code><?php echo $redis_info['pubsub_patterns']; ?></code></td>
		</tr>
		<tr>
			<th>上次fork操作所用时间:</th>
			<td><code><?php echo $redis_info['latest_fork_usec'].' ms'; ?></code></td>
		</tr>
		<tr>
			<th>缓存套接字:</th>
			<td><code><?php echo $redis_info['migrate_cached_sockets']; ?></code></td>
		</tr>
		<tr>
			<th>实例角色:</th>
			<td><code><?php echo $redis_info['role']; ?></code></td>
		</tr>
		<tr>
			<th>已连接从库:</th>
			<td><code><?php echo $redis_info['connected_slaves']; ?></code></td>
		</tr>
		<tr>
			<th>master复制偏移量:</th>
			<td><code><?php echo $redis_info['master_repl_offset']; ?></code></td>
		</tr>
		<tr>
			<th>启用复制日志:</th>
			<td><code><?php echo $redis_info['repl_backlog_active']; ?></code></td>
		</tr>
		<tr>
			<th>复制日志大小:</th>
			<td><code><?php echo $redis_info['repl_backlog_size']; ?></code></td>
		</tr>
		<tr>
			<th>复制日志首位偏移:</th>
			<td><code><?php echo $redis_info['repl_backlog_first_byte_offset']; ?></code></td>
		</tr>
		<tr>
			<th>累积数据量大小:</th>
			<td><code><?php echo $redis_info['repl_backlog_histlen']; ?></code></td>
		</tr>
		<tr>
			<th>主进程已消耗CPU:</th>
			<td><code><?php echo $redis_info['used_cpu_sys']; ?></code></td>
		</tr>
		<tr>
			<th>后台进程已消耗CPU:</th>
			<td><code><?php echo $redis_info['used_cpu_sys_children']; ?></code></td>
		</tr>
		<tr>
			<th>集群模式:</th>
			<td><code><?php echo $redis_info['cluster_enabled']; ?></code></td>
		</tr>
		<tr>
			<th>key数量:</th>
			<td><code><?php echo $redis_info['db0']['expires']; ?></code></td>
		</tr>
		<tr>
			<th>key数量(生存期):</th>
			<td><code><?php echo $redis_info['db0']['avg_ttl']; ?></code></td>
		</tr>
	</table>
</div>
<?php
		}
	}
	function sp_redis_server_get_key(){
		$predis_url = get_option('sp_redis_predis');
		include($predis_url);
		$redis = new Predis\Client('');		
		$redis_keys = $redis->keys('*');
		?>
		<div class="wrap">
			<h1>key列表</h1>
			<table class="form-table">
				<tr>
					<th>Key:</th>
					<td>
						<?php foreach ($redis_keys as $key){ ?>
							<code><?php echo $key; ?></code><br>
						<?php } ?>
					</td>
				</tr>
			</table>
		</div>
	<?php
	}
} // #class
?>